package com.weiaibenpao.demo.chislim.ui.activity.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.bean.UserDataBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.LoginPresenter;
import com.weiaibenpao.demo.chislim.ui.activity.me.CompleteInfoActivity;
import com.weiaibenpao.demo.chislim.utils.SystemUtil;
import com.weiaibenpao.demo.chislim.utils.Utils;
import com.weiaibenpao.demo.chislim.view.LoginView;

import butterknife.BindView;
import butterknife.OnClick;

import static com.weiaibenpao.demo.chislim.db.preferences.UserPreferences.USER_AVATAR;
import static com.weiaibenpao.demo.chislim.db.preferences.UserPreferences.USER_NAME;

public class BindingActivity  extends BaseActivity<LoginView, LoginPresenter> implements LoginView {

    @BindView(R.id.userPhone)
    EditText userPhone;
    @BindView(R.id.userCode)
    EditText userCode;
    @BindView(R.id.sendCode)
    TextView sendCode;
    @BindView(R.id.myLogin)
    Button myLogin;


    UserPreferences userPreferences;
    private String openId,userSource;

    public static void startSelf(Context context,String opendId,String userSource){

       Intent intent = new Intent(context, BindingActivity.class);
       intent.putExtra(UserPreferences.USER_OPENID,opendId);
       intent.putExtra(UserPreferences.USER_SOURCE,userSource);
       context.startActivity(intent);
   }

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_binding;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        userPreferences = UserPreferences.getInstance();
        openId = getIntent().getStringExtra(UserPreferences.USER_OPENID);
        userSource = getIntent().getStringExtra(UserPreferences.USER_SOURCE);

    }

    @OnClick({R.id.sendCode, R.id.myLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sendCode:
                if(checkPhone()){
                    //发送短信验证码
                    mPresenter.getSeedCode(userPhone.getText().toString().trim());
                }
                break;
            case R.id.myLogin:
                goLogin();
                break;
        }
    }
    private void goLogin() {
        if (!TextUtils.isEmpty(userCode.getText().toString().trim())&&!TextUtils.isEmpty(userPhone.getText().toString())){

            mPresenter.bindingPhone(userPreferences.getString(USER_AVATAR),userPreferences.getString(USER_NAME),
                   openId,userSource
                    ,userPhone.getText().toString().trim(),userCode.getText().toString().trim(), SystemUtil.getSystemModel());
        }else {
            Toast.makeText(this, "请输入正确的信息", Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    public void upDataInfoSuccess(UserBean userBean) {


    }

    @Override
    public void loginSuccess(UserDataBean userDataBean) {
        //绑定成功,完善个人信息
        userPreferences.putUserData(userDataBean.getUser(),true);
        CompleteInfoActivity.startSelf(this);
        finish();
    }



    @Override
    public void seedSuccess(String str) {

    }



    /**
     * 电话号码验证
     */
    private TimeCount countDown;
    public boolean checkPhone(){
        //判断手机号码是否为空
        if (!TextUtils.isEmpty(userPhone.getText().toString())){
            //验证手机号码的正确性
            if(Utils.isMobileNO(userPhone.getText().toString().trim())){
                //60秒倒计时
                countDown = new TimeCount(60000, 1000);
                countDown.start();
                return true;
            }else{/**/
                Toast.makeText(this,this.getResources().getString(R.string.input_rightphone_text),Toast.LENGTH_SHORT).show();
                return false;
            }
        }else {
            Toast.makeText(this,this.getResources().getString(R.string.input_rightphone_text),Toast.LENGTH_SHORT).show();
            return false;
        }
    }



    /**
     * 60秒倒计时
     */
    class TimeCount extends CountDownTimer {

        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            sendCode.setClickable(false);
            sendCode.setText(getApplication().getResources().getString(R.string.show_verify_text) + millisUntilFinished / 1000);
        }

        @Override
        public void onFinish() {
            sendCode.setText(getApplication().getResources().getString(R.string.get_repeatverify_text));
            sendCode.setClickable(true);
        }
    }


    @Override
    public LoginPresenter initPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void onDestroy() {
        if(countDown != null){
            countDown.cancel();
        }
        super.onDestroy();
    }

}
